const path = require('path')
const chalk = require('chalk')
const semver = require('semver')
const shell = require('shelljs')

const packageConfig = require('../package.json')

function exec (cmd) {
  return require('child_process').execSync(cmd).toString().trim()
}

const versionRequirements = []

if(shell.which('node')){
  versionRequirements.push({
    name: 'node',
    currentVersion: exec('node -v'),
    versionRequirement: packageConfig.engines.node
  })
}

if(shell.which('npm')){
  versionRequirements.push({
    name: 'npm',
    currentVersion: exec('npm -v'),
    versionRequirement: packageConfig.engines.npm
  })
}


module.exports = function(){
  versionRequirements.forEach(mod=>{
    if(!semver.satisfies(mod.currentVersion, mod.versionRequirement)){
      console.log(`${mod.name}: ${chalk.red(mod.currentVersion)} should be ${chalk.green(mod.versionRequirement)}`)
      process.exit(1)
    }
  })
}

